<?php

/**
 * Class WjbwebAction
 * 企业pc网站 后台编辑
 */
class WjbwebAction extends UserAction{

    protected  $sitemap_dir = 'tpl/web/resource/sitemap/';

    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $menu = M('web_menu')->where(array('token'=>$this->token) )->select();

        $this->assign('menu',$menu);
        $this->display();
    }

    public function menuadd(){
        if(IS_POST){
            $data = array();
            $data['name'] = $_POST['name'];
            $data['sysmodel'] = $_POST['sysmodel'];
            $data['pid'] = $_POST['pid'];
            $data['keywords'] = $_POST['keywords'];
            $data['description'] = $_POST['description'];
            $data['token'] = $this->token;

            if($data['name'] !== '' && $data['sysmodel'] !== ''  && $data['token'] !== '' ){

                M('web_menu')->add($data);
                $this->success("操作成功");
                exit;
            }
            $this->error("数据不完整");
            exit;
        }

        $sysmodel = M('web_sysmodel')->field('id,cnname,enname,info')->select();
        $pmenu = M('web_menu')->where(array('sysmodel'=>'pmenu','token'=>$this->token))->select();
        //将这个内容作为一个json字符串发送到客户端 这样就不用ajax来回调用了 因为界面上要展示用户选择了一个之后 出现的模型说明  页面类型 不需要选
        $this->assign('sysmodel',$sysmodel);
        $sysmodelJSON = json_encode($sysmodel);

        $this->assign('pmenu',$pmenu);
        $this->assign('sysmodelJSON',$sysmodelJSON);

        $this->display();
    }

    /**
     * 删除目录需要删除该目录下的所有内容
     * 看样子这个函数要随着系统模型的增加而改变了
     */
    public function menudel() {

        $id = isset($_GET['id'])?$_GET['id']:'0';
        $token = $_GET['token'];
        //判断该栏目下是否还有内容
        $menu = M('web_menu')->where(array('token'=>$token,'id'=>$id))->find();
        if(!$menu){
            exit("错误");
        }
        if( $menu['sysmodel'] == 'article' ) {
            $count = M('web_article')->where(array('token'=>$token,'menu'=>$id))->count();
            if( $count ){
                exit('该栏目下有内容 请先删除内容');
            }else{
                $res = M('web_menu')->where(array('token'=>$token,'id'=>$id))->delete();
                if($res) exit("1");
            }
        }else if($menu['sysmodel'] == 'pmenu' || $menu['sysmodel'] == 'wxarticle' ){
            $res = M('web_menu')->where(array('token'=>$token,'id'=>$id))->delete();
            if($res) exit("1");
        }

        exit('错误');

    }

    public function edit(){
        $id = $_GET['id'];
        if( IS_POST ) {
            $data = array();
            $data['name'] = $_POST['name'];
            $data['keywords'] = $_POST['keywords'];
            $data['description'] = $_POST['description'];
            $data['token'] = $this->token;

            if($data['name'] !== '' && $data['sysmodel'] !== ''  && $data['token'] !== '' ){

                $res = M('web_menu')->where(array('id'=>$id))->save($data);
                if($res) {
                    $this->success("操作成功");
                }else{
                    $this->error("操作失败");
                }
                exit;
            }
            $this->error("数据不完整");
            exit;
        }
        $menu = M('web_menu')->where(array('id'=>$id))->find();
        $pmenu = M('web_menu')->where(array('id'=>$menu['pid']))->find();

        $sysmodel = M('web_sysmodel')->where(array('enname'=>$menu['sysmodel']))->find();

        $this->assign('menu',$menu);
        $this->assign('sysmodel',$sysmodel);
        $this->assign('pmenu',$pmenu);

        $this->display();
    }


    public function template(){
        //要列出每个栏目下面的各个类型的页面  所以 页面的类型是不是还有一个数据 做一下说明  看看情况

        //每个栏目下的各个页面 要根据系统模型来列举 首先
        $menus = M('web_menu')->where(array('token'=>$this->token))->select();
        if( !$menus ){
            $this->display();
        }

        $sysmodelArr = array();
        $sysmodelStr = '';
        foreach( $menus as $val ){
            $sysmodelStr .= '\''.$val['sysmodel'].'\''.',';
        }
        $sysmodelStr = trim($sysmodelStr,',');

        $sysmodel = M('web_sysmodel')->where('enname in ('.$sysmodelStr.')')->select();

        //将模型英文名作为键 方便前端调用
        $newSysmodel = array();

        foreach($sysmodel as $val ){
            $val['pagetype'] = unserialize($val['pagetype']);
            $newSysmodel[$val['enname']] = $val;

        }

        $this->assign('sysmodel',$newSysmodel);


        // 根据目录 目录中带有系统模型  列出模板   使用$menus 与后台的导航栏目重复了  不好
        $this->assign('menu', $menus);

        $this->display();
    }

    public function tempedit(){



        $this->display();

    }


    /**
     * 发布主题
     */
    public function addpost(){
        if(IS_POST){
            $data = array();
            $data['title']= $_POST['title'];
            $data['author'] = $_POST['author'];
            $data['info'] = $_POST['info'];
            $data['menu'] = $_POST['menu'];
            $data['thumb'] = $_POST['thumb'];
            $data['content'] = $_POST['content'];
            if(!$data['title'] || !$data['content'] || !$data['menu']){
                $this->error('标题和内容是必填项 或者没有选择栏目');
            }else {
                $data['createtime'] = time();
                $data['token'] = $this->token;
                M('web_article')-> add($data);
                $this->success('提交成功');
            }
        }else {
            $menus = M('web_menu')->where(array('token' => $this->token, 'sysmodel' => 'article'))->select();

            $this->assign('themenu', $menus);

            $this->display();
        }

    }

    public function editpost(){
        $id = $_GET['id'];

        if(!$id){
            $this->error('访问出错');
        }

        if(IS_POST){
            $data = array();
            $data['title'] = $_POST['title'];
            $data['author'] = $_POST['author'];
            $data['info'] = $_POST['info'];
            $data['menu'] = $_POST['menu'];
            $data['thumb'] = $_POST['thumb'];
            $data['keywords'] = $_POST['keywords'];
            $data['description'] = $_POST['description'];
            $data['content'] = $_POST['content'];
            if(!$data['title'] || !$data['content']){
                $this->error('标题和内容是必填项');
            }else{
                $data['updatetime'] = time();
                $data['token'] = $this->token;
                M('web_article')-> where(array('id'=>$id,'token'=>$this->token ) )->save($data);
                $this->success('修改成功');
            }
        }else {
            $menus = M('web_menu')->where(array('token' => $this->token, 'sysmodel' => 'article'))->select();
            $article = M('web_article')->where(array('token' => $this->token, 'id' => $id))->find();

            $this->assign('themenu', $menus);
            $this->assign('article', $article);
            $this->display();
        }

    }

    /**
     * 删除主题
     */
    public function del(){
        $id = $_GET['id'];
        if(!$id){
            exit();
        }else{
            M('web_article')->where(array('id'=>$id,'token'=>$this->token))->delete();
            exit("1");
        }
    }

    /**
     *主题
     */
    public function post(){
        $menu = M('web_menu')->where(array('token'=>$this->token))->select();
        $mid = isset($_GET['mid'])?$_GET['mid']:'0';

        import('ORG.Util.Page');

        if(!$mid){
            $count = M('web_article')->where(array('token'=>$this->token))->count();
            $page = new Page($count);
            $show = $page->show();
            $list = M('web_article')->where(array('token'=>$this->token))->limit($page->firstRow.','.$page->listRows)->select();
        }else{
            $count = M('web_article')->where(array('token'=>$this->token,'menu'=>$mid))->count();
            $page = new Page($count);
            $show = $page->show();
            $list = M('web_article')->where(array('token'=>$this->token,'menu'=>$mid))->limit($page->firstRow.','.$page->listRows)->select();
        }



        $this->assign('menu',$menu);
        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->assign('mid',$mid);
        $this->display();

    }


    /**
     * 产品列表
     */
    public function productmanager() {

        //分页
        $productlist = M('web_product')->where(array( 'token'=> $this->token))->select();

        $this->assign( 'productlist', $productlist );

        $this->display();
    }


    /**
     * 添加产品
     */
    public function addproduct() {
        if( IS_POST ){

            $data = array();
            $data['token'] = $this->token;
            $data['number'] = $_POST['number'];
            $data['name'] = $_POST['name'];
            $data['category'] = $_POST['category'];
            $data['price'] = $_POST['price'];
            $data['photo'] = $_POST['photo'];
            $data['type'] = $_POST['type'];
            $data['link'] = $_POST['link'];
            $data['keywords'] = $_POST['keywords'];
            $data['description'] = $_POST['description'];
            $data['content'] = $_POST['content'];
            $data['createtime'] = time();
            $res = M('web_product')->add($data);
            if( $res ) {
                $this->success("提交成功");
            }else{
                $this->error("操作失败");
            }

        }else {
            $classify = M('web_product_cat')->where(array('token' => $this->token))->select();

            $this->assign('classify', $classify);

            $this->display();
        }
    }

    /**
     *编辑
     */
    public function editproduct() {

        $id = $_GET['id'];

        if( IS_POST ) {
            $data = array();
            $data['token'] = $this->token;
            $data['number'] = $_POST['number'];
            $data['name'] = $_POST['name'];
            $data['category'] = $_POST['category'];
            $data['price'] = $_POST['price'];
            $data['photo'] = $_POST['photo'];
            $data['type'] = $_POST['type'];
            $data['link'] = $_POST['link'];
            $data['keywords'] = $_POST['keywords'];
            $data['description'] = $_POST['description'];
            $data['content'] = $_POST['content'];
            $data['updatetime'] = time();
            $res = M('web_product')->where( array( 'id'=>$id ) )->save($data);
            if( $res ) {
                $this->success("提交成功");
            }else{
                $this->error("操作失败");
            }

        }else {

            $product = M('web_product')->where(array('token' => $this->token, 'id' => $id))->find();
            $classify = M('web_product_cat')->where(array('token' => $this->token))->select();

            $this->assign('classify', $classify);
            $this->assign('product', $product);
            $this->assign('id', $id);

            $this->display();
        }
    }

    /**
     * 删除产品
     */
    public function delproduct(){
        $id = $_GET['id'];
        if(!$id){
            exit();
        }else{
            M('web_product')->where(array('id'=>$id,'token'=>$this->token))->delete();
            exit("1");
        }
    }

    /**
     * 删除分类
     */
    public function delclassify(){
        $id = $_GET['id'];
        if(!$id){
            exit();
        }else{
            //查找该分类有没有内容 有没有子类
            $res = M('web_product_cat')->where(array('pid'=>$id,'token'=>$this->token))->find();
            if( $res ){
                exit("请先删除下级子栏目");
            }
            $res = M('web_product')->where(array('token'=>$this->token,'category'=>$id))->find();
            if( $res ){
                exit("改分类下有产品 请先删除所有的产品");
            }

            M('web_product_cat')->where(array('id'=>$id,'token'=>$this->token))->delete();
            exit("1");
        }
    }
    /**
     * 产品分类管理
     */
    public function productclassify() {

        $classify = M('web_product_cat')->where(array( 'token'=>$this->token ))->select();
        $classifyNew = array();
        foreach( $classify as $val ) {
            $classifyNew[$val['id']] = $val;
        }

        $this->assign( 'classify', $classifyNew );
        $this->display();

    }

    /**
     * 编辑产品类目
     */
    public  function editclassify(){
        $id = $_GET['id'];
        if( IS_POST ) {
            $data = array();
            $data['id'] = $id;
            $data['token'] = $this->token;
            $data['categoryname'] = $_POST['categoryname'];
            $data['pid'] = $_POST['pid'];
            $data['icon'] = $_POST['icon'];

            $res = M('web_product_cat')->where( array( 'id'=>$id ) )->save( $data );
            if( $res ){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }

        }else {

            $classify = M('web_product_cat')->where(array('token' => $this->token, 'id' => $id))->find();

            $pidStr = $classify['pid'];
            $parentClassify = array('id' => 0);//默认所属类别为根类 如果搜索不到
            if ($classify['pid']) {

                $parentClassify = M('web_product_cat')->where(array('token' => $this->token, 'id' => $classify['pid']))->find();
                $pidStr .= ',' . $parentClassify['pid'];
            }

            //同pid 同父pid
            //temp   可以作为新的父类的列表
            $temp = M('web_product_cat')->where(array('token' => array('eq', $this->token), 'pid' => array('in', $pidStr)))->select();
            for ($i = 0; $i < count($temp); $i++) {
                if ($temp[$i]['id'] == $id) {
                    unset($temp[$i]);
                }
            }

            $this->assign('classify', $classify);
            $this->assign('parentclassify', $parentClassify);
            $this->assign('classifylist', $temp);
            $this->assign( 'id', $id );
            $this->display();
        }
    }


    /**
     * 添加产品类目
     */
    public function classifyadd(){

        if( $_POST ){
            $data = array();
            $data['token'] = $this->token;
            $data['categoryname'] = $_POST['categoryname'];
            $data['pid'] = $_POST['pid'];
            $data['icon'] = $_POST['icon'];
            M('web_product_cat')->add($data);
            $this->success("添加成功");
        }else{
            $classifyList = M('web_product_cat')->where(array('token'=>$this->token))->select();

            $this->assign( 'classifylist' , $classifyList );

            $this->display();
        }

    }


    /**
     * 网站基础信息设置
     */
    public function webset(){

        if( IS_POST ){
            $id = $_POST['id'];
            $data['webname'] = $_POST['webname'];
            $data['webkeywords'] = $_POST['webkeywords'];
            $data['webdescription'] = $_POST['webdescription'];
            $res = M('web_domain')->where(array('id'=>$id))->save($data);
            if( $res ){
                $this->success("操作成功");
            }else{
                $this->error("操作失败");
            }
            exit;
        }else {
            $web = M('web_domain')->where(array('token' => $this->token))->find();
            if (!$web) {
                $this->error("你还没有绑定域名");
                exit;
            }
            $this->assign( 'web', $web );
        }

        $this->display();
    }


    /**
     *
     */
    public function sitemap(){

        $file = $this->token.'.xml';

        $root_path = dirname(dirname(THINK_PATH));//网站根路径
        $sitemap = $this->sitemap_dir.$file;//网站相对路径
        $path = $root_path.DIRECTORY_SEPARATOR.$sitemap;//绝对路径

        if(IS_POST){
            $xml = $_POST['content'];
            $res = preg_match_all('/(\?php)|(script)/',$xml,$match);
            if($res){
                $this->error('不能使用脚本 请使用正规sitemap格式');
                exit;
            }else{
                file_put_contents($sitemap,$xml);
                $this->success("设置成功");
                exit;
            }
        }

        $web = M('web_domain')->where(array('token'=>$this->token))->find();


        if( file_exists($path) ){
            $xml = file_get_contents($sitemap);
        }else{
            $xml = '';
            file_put_contents($sitemap,$xml);
        }

        $this->assign('token',$this->token);

        $this->assign( 'xml', $xml );
        $this->assign( 'web', $web );

        $this->display();

    }


    /**
     * 表单的操作
     */
    public function formcontrol(){
        $res = M('web_form')->select();
        $this->assign('formlist',$res);
        $this->display();
    }

    public function formshow(){

        $id = $_GET['id'];
        $res = M('web_form')->find( array( 'id' => $id ) );

        $dataformat = unserialize($res['dataformat']);
        $dataformat = $dataformat['dataformat'];

        $formlist = M('web_formdata')->where(array('token'=>$this->token,'formid'=>$id))->select();

        $formlistdata = array();
        foreach($formlist as $val){
            $data = unserialize($val['formdata']);
            $str = '';
            foreach($dataformat as $key => $v ){
                $str .= $v.":".$data[$key].'|';
            }
            $str = mb_strcut($str,0,120,'utf8')."...";
            $formlistdata[$val['id']] = $str;
        }

        $this->assign( 'formlistdata', $formlistdata );
        //看来还是需要 数据结构的 要不然 不知道 哪个字段是什么意思
        $this->assign( 'formlist', $formlist );
        $this->assign('formid',$id);

        $this->display();
    }

    /**
     * 单独每一个表单的查看
     */
    public function formdata(){
        $id = $_GET['id'];
        $formid = $_GET['formid'];

        $res = M('web_form')->where(array('id'=>$formid))->find();

        $dataformat = unserialize($res['dataformat']);

        $dataformat = $dataformat['dataformat'];

        $info = M("web_formdata")->where(array('id'=>$id,'token'=>$this->token))->find();

        $formdata = unserialize($info['formdata']);
        unset($formdata['__hash__']);

        $this->assign('id',$formid);

        $this->assign( 'dataformat', $dataformat );

        $this->assign( 'formdata', $formdata );

        $this->display();
    }




}